import COS from 'cos-wx-sdk-v5';

export default class UniToCos {
	bucket : string
	region : string
	cos : any

	constructor(bucket : string, region : string) {
		this.bucket = bucket
		this.region = region
	}

	setBucket(val : string) {
		this.bucket = val
	}

	setRegion(val : string) {
		this.region = val
	}

	createCos(secretId : string, secretKey : string, xCosSecurityToken : string) {
		this.cos = new COS({
			SecretId: secretId,
			SecretKey: secretKey,
			XCosSecurityToken: xCosSecurityToken
		})
	}
	
	wxUploadFile(filePath:string, fileName:string) {
		return new Promise((resolve, reject) => {
			this.cos.postObject({
				Bucket: this.bucket,
				/* 必须 */
				Region: this.region,
				/* 必须 */
				Key: `${fileName}`,
				/* 必须 */
				FilePath: filePath,
				onProgress: function (info) {
					console.log("[cos.postObject-seccess]", JSON.stringify(info));
				}
			}, function (err, data) {
				if(err){
					reject(err)
				}
				if (data) {
					resolve(data)
				}
			});
		})
	}
}